package com.example.util;

/**
 * WebEx WebLogic server JDK is still 1.4.2.
 * So we can not use JDK 5.0 class MyStringBuilder
 * 
 * We created our own class StringBuidler here.
 * It's performance is better than StringBuffer
 * 
 * (StringBuffer 3000ms, MyStringBuilder 2300ms)
 * */
public final class MyStringBuilder
    implements java.io.Serializable
{
    private char value[];
    private int count;

    static final long serialVersionUID = 100L;

    public MyStringBuilder() {
    	this(64);
    }

    public MyStringBuilder(int length) {
	value = new char[length];
	count = 0;
    }

    public MyStringBuilder(String str) {
	this(str.length() + 32);
	append(str);
    }

    public  int length() {
	return count;
    }

    public void setLength(int length) {
    	this.count = length;
    }
    
    public int capacity() {
	return value.length;
    }

    public void ensureCapacity(int minimumCapacity) {
	if (minimumCapacity > value.length) {
	    expandCapacity(minimumCapacity);
	}
    }


    private void expandCapacity(int minimumCapacity) {
    	//if (WbxAuth.canLD())
    	//	WbxAuth.debug("expandCapacity(" + minimumCapacity + ")");
	int newCapacity = (value.length + 1) * 2;
        if (newCapacity < 0) {
            newCapacity = Integer.MAX_VALUE;
        } else if (minimumCapacity > newCapacity) {
	    newCapacity = minimumCapacity;
	}
	
	char newValue[] = new char[newCapacity];
	System.arraycopy(value, 0, newValue, 0, count);
	value = newValue;
    }


    public MyStringBuilder append(Object obj) {
	return append(String.valueOf(obj));
    }

    public MyStringBuilder append(String str) {
	if (str == null) {
	    str = String.valueOf(str);
	}

	int len = str.length();
	int newcount = count + len;
	if (newcount > value.length)
	    expandCapacity(newcount);
	str.getChars(0, len, value, count);
	count = newcount;
	return this;
    }

    public MyStringBuilder append(MyStringBuilder sb) {
	if (sb == null) {
	    sb = nullSB;
	}
	return append(sb.value, 0, sb.count);
    }

    private static final MyStringBuilder nullSB =  new MyStringBuilder("null");

    public final MyStringBuilder append(char str[]) { 
	int len = str.length;
	int newcount = count + len;
	if (newcount > value.length)
	    expandCapacity(newcount);
	System.arraycopy(str, 0, value, count, len);
	count = newcount;
	return this;
    }

    public MyStringBuilder append(char str[], int offset, int len) {
        int newcount = count + len;
	if (newcount > value.length)
	    expandCapacity(newcount);
	System.arraycopy(str, offset, value, count, len);
	count = newcount;
	return this;
    }

    public MyStringBuilder append(boolean b) {
        if (b) {
            int newcount = count + 4;
            if (newcount > value.length)
                expandCapacity(newcount);
            value[count++] = 't';
            value[count++] = 'r';
            value[count++] = 'u';
            value[count++] = 'e';
        } else {
            int newcount = count + 5;
            if (newcount > value.length)
                expandCapacity(newcount);
            value[count++] = 'f';
            value[count++] = 'a';
            value[count++] = 'l';
            value[count++] = 's';
            value[count++] = 'e';
        }
	return this;
    }

    public final MyStringBuilder append(char c) {
	if (count >= value.length)
	    expandCapacity(count + 1);
	value[count++] = c;
	return this;
    }

    public MyStringBuilder append(int i) {
    	append(Integer.toString(i));
        return this;
    }

    public MyStringBuilder append(long l) {
        append(Long.toString(l));
        return this;
    }

    public final MyStringBuilder append(float f) {
        return append(new Float(f).toString());
    }

    public final MyStringBuilder append(double d) {
        return append(Double.toString(d));
    }

    public final String toString() {
	return new String(this.value, 0, count);
    }

    public final String toString(int startIndex) {
    	return new String(this.value, startIndex, count - startIndex);
    }

    public final char getChar(int p) {
    	return value[p];
    }

}
